import json
from datetime import datetime
from typing import Optional, Any

from fastapi_amis_admin.models import Field
from passlib.context import CryptContext
from pydantic import BaseModel
from sqlmodel import SQLModel
from fastapi.encoders import jsonable_encoder
from starlette.responses import JSONResponse

now = datetime.now()
print(now.strftime("%Y%m%d"))
print(now.strftime("%Y-%m-%d %H:%M:%S"))

pwd_context = CryptContext(schemes=["bcrypt"], deprecated="auto")


def verify_password(plain_password, hashed_password):
    return pwd_context.verify(plain_password, hashed_password)


def get_password_hash(password):
    return pwd_context.hash(password)


print(get_password_hash("123456"))


def custom_jsonable_encoder(v: Any) -> Any:
    if isinstance(v, datetime):
        return v.isoformat()
    if isinstance(v, BaseModel):
        # 如果v是Pydantic模型，可以递归地编码其属性
        return {key: custom_jsonable_encoder(val) for key, val in v.dict().items()}
    # 对于其他类型，可以保持默认行为或添加更多自定义编码逻辑
    return v


class AppBaseModel(SQLModel):
    id: Optional[int] = Field(default=0, primary_key=True, nullable=False, title='id', exclude=False)
    name: Optional[str] = Field(title='姓名')
    creator: Optional[int] = Field(title='创建者')
    create_date: Optional[datetime] = Field(index=True, title='创建时间',
                                            serialization_alias="create_date", validation_alias="create_date",
                                            alias="create_date")


user = AppBaseModel(id=123, name="abc", creator=123, create_date=datetime.now())
print(user.json)
map = dict()
for attr in dir(user):
    if not attr.startswith("_"):
        if isinstance(getattr(user, attr), datetime):
            map[attr] = type(getattr(user, attr))
            # print(f"{attr}   {type(getattr(user, attr))}")

a = jsonable_encoder(user, custom_encoder={
    datetime: lambda obj: obj.strftime("%Y-%m-%d")
})
print(type(a))
print(a)
for key, value in a.items():
    print(f"{key}  {value}")
    if map.get(key):
        a[key] = dict(user)[key].strftime("%Y-%m-%d %H:%M:%S")
# a['createDate']=user.create_date.strftime("%Y-%m-%d %H:%M:%S")
print(a)
print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"))




print(jsonable_encoder(a, custom_encoder={
    datetime: lambda obj: obj.strftime("%Y-%m-%d")
}))
